package appapi.mapper;


import appapi.Request.DeviceBlackBoxPageReq;
import appapi.Request.bridge.BlackBoxListReq;
import appapi.Request.bridge.RealDataPageReq;
import appapi.Vo.brider.BlackBoxVO;
import appapi.Vo.brider.DeviceVO;
import appapi.entity.bridge.IotDevice;
import appapi.entity.bridge.vo.ProjectsVo;
import appapi.entity.record.RealDataEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;


/**
 * @author hlm
 * @date
 * @description: 黑匣子
 */
@Mapper
public interface DeviceBlackBoxMapper extends BaseMapper<IotDevice> {

    List<DeviceVO> selectDeviceList(@Param("req") DeviceBlackBoxPageReq req);

    Integer updateDeviceStatus(@Param("deviceSn") String deviceSn,@Param("deviceStatus")String deviceStatus,@Param("updateTime") LocalDateTime updateTime);


    List<BlackBoxVO> getBlackBoxList(@Param("req") BlackBoxListReq req);

    @Select("SELECT * FROM iot_device i " +
            "JOIN project p ON i.proj_id = p.proj_id " +
            "JOIN organization o ON p.orga_id = o.orga_id " +
            "WHERE o.orga_id = #{orgaId}")
    List<IotDevice> list(@Param("orgaId") Integer orgaId);

    @Select("SELECT * FROM iot_device i " +
            "JOIN project p ON i.proj_id = p.proj_id " +
            "JOIN organization o ON p.orga_id = o.orga_id " +
            "WHERE o.orga_id = #{orgaId} and i.device_sn = #{deviceSn}")
    List<IotDevice> lister(@Param("orgaId") Integer orgaId,@Param("deviceSn") String deviceSn);

    List<IotDevice> selectSceneDeviceList(@Param("sceneId") Long sceneId);

    Integer selectSceneDeviceOnlineCount(@Param("sceneId") Long sceneId);

    @Select("SELECT COUNT(*) FROM iot_device i " +
            "JOIN project p ON i.proj_id = p.proj_id " +
            "JOIN organization o ON p.orga_id = o.orga_id " +
            "WHERE o.orga_id = #{orgaId}")
    Integer counts(@Param("orgaId") Integer orgaId);


    @Select("SELECT COUNT(*) FROM iot_device i " +
            "JOIN project p ON i.proj_id = p.proj_id " +
            "JOIN organization o ON p.orga_id = o.orga_id " +
            "WHERE o.orga_id = #{orgaId} " +
            "AND i.device_online = 0 " +
            "AND i.device_sn = #{deviceSn}")
    Integer countByDeviceStatusAndOrgaId(@Param("orgaId") Integer orgaId, @Param("deviceSn") String deviceSn);

    Integer insertRealData(RealDataEntity entity);
}

